home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_500 / wiconify / wiconsetter.lzh / wIconCutter / wIconCutter.c < prev    next >
C/C++ Source or Header  |  1991-04-19  |  6KB  |  280 lines

  1. /*
  2.  *  WICONCUTTER     A companion utility to wIconSetter and wIconify.
  3.  *                  wIconCutter lets you select a section of any screen
  4.  *                  and creates a wIconSetter icon file using the selected
  5.  *                  bitmap as the icon's image.
  6.  *
  7.  *  Copyright 1990 by Davide P. Cervone, all rights reserved.
  8.  *  You may use this code, provided this copyright notice is kept intact.
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <graphics/rastport.h>
  13. #include "Frame.h"
  14.  
  15.  
  16. char *program    = "wIconCutter";
  17. char *version    = "v1.1";
  18. char *copyright  = "Copyright (c) 1990 by Davide P. Cervone";
  19. char *usage =
  20.    "wIconCutter [IMAGE | SELECT] [MASK] [APPEND] [PLANES n] file";
  21.  
  22. #ifdef MANX
  23.    #define ERROR errno
  24. #else
  25.    #define ERROR _OSERR
  26. #endif
  27.  
  28. extern long fopen();
  29. extern long ERROR;
  30.  
  31. static char *FileName;      /* the output file name */
  32. static long OutFile;        /* the output file handle */
  33.  
  34. static char *Hex = "0123456789abcdef)!@#$%^&*(ABCDEF";
  35. #define MAXHEX      32
  36. #define MAXLINE     132
  37.  
  38. static int AppendIt;        /* TRUE if appending to the output file */
  39. static int ImageType;       /* which type of image is being created */
  40. #define CUT_IMAGE   1
  41. #define CUT_SELECT  2
  42. #define CUT_MASK    4
  43.  
  44.  
  45. #define ARGMATCH(s)     (stricmp(argv[0],s) == 0)
  46.  
  47.  
  48. /*
  49.  *  ParseArguments()
  50.  *
  51.  *  Set the function to framing
  52.  *  While there are more arguements to consider
  53.  *    Get the next one
  54.  *    If it is an image type, add it to the type to be produced
  55.  *    Otherwise if it is APPEND, set the append flag
  56.  *    Otherwise if it is PLANES,
  57.  *      Get a bit-plane mask
  58.  *    Otherwise, if no filename has been given use it as a file name
  59.  *    Otherwise, give an error and show usage
  60.  *  If no file was specified, give an error and show usage
  61.  *  return the function to be performed
  62.  */
  63.  
  64. int ParseArguments(argc,argv)
  65. int argc;
  66. char **argv;
  67. {
  68.    int function = FRAME_IT;
  69.    long mask;
  70.    
  71.    while (--argc > 0)
  72.    {
  73.       argv++;
  74.       if (ARGMATCH("IMAGE"))  ImageType |= CUT_IMAGE; else
  75.       if (ARGMATCH("SELECT")) ImageType |= CUT_SELECT; else
  76.       if (ARGMATCH("MASK"))   ImageType |= CUT_MASK; else
  77.       if (ARGMATCH("APPEND")) AppendIt = TRUE; else
  78.       if (ARGMATCH("PLANES"))
  79.       {
  80.          if (argc >= 2 &&  (sscanf(argv[1],"%lx",&mask) == 1))
  81.          {
  82.             PlaneMask = mask;
  83.          } else {
  84.             printf("PLANES requires a hex value\n");
  85.             function = JUST_EXIT;
  86.          }
  87.          argc--; argv++;
  88.       } else
  89.       if (FileName == NULL) FileName = argv[0]; else
  90.       {
  91.          printf("Unrecognized parameter '%s'\n",argv[0]);
  92.          function = SHOW_USAGE;
  93.       }
  94.    }
  95.    if (FileName == NULL)
  96.    {
  97.       printf("Missing file specification\n");
  98.       function = SHOW_USAGE;
  99.    }
  100.    return(function);
  101. }
  102.  
  103.  
  104. /*
  105.  *  WriteHeading()
  106.  *
  107.  *  Write the comments at the top of the file that identify the file and
  108.  *  the creating program.
  109.  */
  110.  
  111. static void WriteHeading()
  112. {
  113.    fprintf(OutFile,"/*\n *  %s\n *\n",FileName);
  114.    fprintf(OutFile," *  An Icon Created by %s %s\n */\n",program,version);
  115. }
  116.  
  117.  
  118. /*
  119.  *  WriteBitMap()
  120.  *
  121.  *  For each row of the bitmap
  122.  *    And each column of each row
  123.  *      Get the pen color of the pixel at that position
  124.  *      Save the pixel value as a HEX digit in the output line
  125.  *    End the line and print it to the output file
  126.  */
  127.  
  128. static void WriteBitMap(theRPort)
  129. struct RastPort *theRPort;
  130. {
  131.    short h,w;
  132.    short Width = (theWidth < MAXLINE)? theWidth: MAXLINE;
  133.    int pen;
  134.    char Line[MAXLINE];
  135.  
  136.    for (h=0; h<theHeight; h++)
  137.    {
  138.       for (w=0; w<Width; w++)
  139.       {
  140.          pen = ReadPixel(theRPort,w,h) % MAXHEX;
  141.          Line[w] = Hex[pen];
  142.       }
  143.       Line[w] = 0;
  144.       fprintf(OutFile,"\n   ");
  145.       fprintf(OutFile,Line);
  146.    }
  147.    fprintf(OutFile,"\n");
  148. }
  149.  
  150.  
  151. /*
  152.  *  WriteMask()
  153.  *
  154.  *  Write the MASK command to the file
  155.  *  For each row of the bitmap
  156.  *    And each column of each row
  157.  *      Get the pen color of the pixel at that position
  158.  *      If it is non-zero, put a '1' in the line, otherwise a '0'
  159.  *    End the line and print it to the file
  160.  */
  161.  
  162. static void WriteMask(theRPort)
  163. struct RastPort *theRPort;
  164. {
  165.    short h,w;
  166.    short Width = (theWidth < MAXLINE)? theWidth: MAXLINE;
  167.    int pen;
  168.    char Line[MAXLINE];
  169.  
  170.    fprintf(OutFile,"\nMASK:\n");
  171.    for (h=0; h<theHeight; h++)
  172.    {
  173.       for (w=0; w<Width; w++)
  174.       {
  175.          pen = ReadPixel(theRPort,w,h);
  176.          if (pen) Line[w] = Hex[1]; else Line[w] = Hex[0];
  177.       }
  178.       Line[w] = 0;
  179.       fprintf(OutFile,"\n   ");
  180.       fprintf(OutFile,Line);
  181.    }
  182.    fprintf(OutFile,"\n");
  183. }
  184.  
  185.  
  186.  
  187. /*
  188.  *  WriteImage()
  189.  *
  190.  *  Write the IMAGE command to the file, then print the image data
  191.  */
  192.  
  193. static void WriteImage(theRPort)
  194. struct RastPort *theRPort;
  195. {
  196.    fprintf(OutFile,"\nIMAGE:\n");
  197.    WriteBitMap(theRPort);
  198. }
  199.  
  200.  
  201. /*
  202.  *  WriteSelect()
  203.  *
  204.  *  WRite the SELECT command to the file, then write the image data
  205.  */
  206.  
  207. static void WriteSelect(theRPort)
  208. struct RastPort *theRPort;
  209. {
  210.    fprintf(OutFile,"\nSELECT:\n");
  211.    WriteBitMap(theRPort);
  212. }
  213.  
  214.  
  215. /*
  216.  *  WriteIcon()
  217.  *
  218.  *  If we are appending to the file,
  219.  *    Open the file for appending (error if we can't)
  220.  *  Otherwise
  221.  *    Open the file for writing (error if we can't)
  222.  *    And write the heading
  223.  *  Initialize a RastPort to use the copied BitMap.
  224.  *  Write the IMAGE, SELECT, and MASK data that have been requested.
  225.  *  Close the file
  226.  */
  227.  
  228. static void WriteIcon()
  229. {
  230.    struct RastPort theRPort;
  231.  
  232.    if (AppendIt)
  233.    {
  234.       OutFile = fopen(FileName,"a");
  235.       if (OutFile == NULL)
  236.          DoExit("Can't Append '%s' - Error: %ld",FileName,ERROR);
  237.    } else {
  238.       OutFile = fopen(FileName,"w");
  239.       if (OutFile == NULL)
  240.          DoExit("Can't Create '%s' - Error: %ld",FileName,ERROR);
  241.       WriteHeading();
  242.    }
  243.  
  244.    InitRastPort(&theRPort);
  245.    theRPort.BitMap = &theBitMap;
  246.    if (ImageType & CUT_IMAGE)  WriteImage(&theRPort);
  247.    if (ImageType & CUT_SELECT) WriteSelect(&theRPort);
  248.    if (ImageType & CUT_MASK)   WriteMask(&theRPort);
  249.  
  250.    fclose(OutFile); OutFile = NULL;
  251. }
  252.  
  253.  
  254. /*
  255.  *  UseFrame()
  256.  *
  257.  *  If no image type was specified, assume IMAGE
  258.  *  Write the icon to the file
  259.  */
  260.  
  261. void UseFrame(function)
  262. int function;
  263. {
  264.    if (ImageType == 0) ImageType = CUT_IMAGE;
  265.    WriteIcon();
  266. }
  267.  
  268.  
  269. /*
  270.  *  DoUseExit()
  271.  *
  272.  *  The only cleanup needed here is closing the file if it is open.
  273.  *  The rest of the cleanup is done by Frame itself.
  274.  */
  275.  
  276. void DoUseExit()
  277. {
  278.    if (OutFile) fclose(OutFile);
  279. }
  280.